package com.shunda.ysdgj.manager.service.impl.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.shunda.ysdgj.common.exception.CustomException;
import com.shunda.ysdgj.common.util.ValidateUtil;
import com.shunda.ysdgj.manager.mapper.goods.GoodsSpecItemMapper;
import com.shunda.ysdgj.manager.service.goods.GoodsSpecItemService;
import com.shunda.ysdgj.model.entity.goods.GoodsSpec;
import com.shunda.ysdgj.model.entity.goods.GoodsSpecItem;
import com.shunda.ysdgj.model.vo.ResultCodeEnum;
import com.shunda.ysdgj.model.vo.goods.GoodsSpecItemVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsSpecItemServiceImpl
        extends MPJBaseServiceImpl<GoodsSpecItemMapper, GoodsSpecItem>
        implements GoodsSpecItemService, MPJBaseService<GoodsSpecItem> {

    public List<GoodsSpecItemVo> findVoBySpecId(Long id) {
        MPJLambdaWrapper<GoodsSpecItem> baseWrapper = JoinWrappers
                .lambda(GoodsSpecItem.class)
                .select(GoodsSpecItem::getId, GoodsSpecItem::getSpecValue)
                .eq(GoodsSpecItem::getSpecId, id);
        return getBaseMapper().selectJoinList(GoodsSpecItemVo.class, baseWrapper);
    }

    public List<GoodsSpecItem> findBySpecId(Long id) {
        if (ValidateUtil.isNotLong(id)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        return lambdaQuery().eq(GoodsSpecItem::getSpecId, id).list();
    }

    @Transactional
    public boolean add(List<GoodsSpecItem> goodsSpecItemList) {
        return saveBatch(goodsSpecItemList);
    }

    @Transactional
    public boolean modify(GoodsSpec goodsSpec, List<GoodsSpecItem> specItemList) {
        if (specItemList.isEmpty()) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        for (GoodsSpecItem specItem : specItemList) {
            if (specItem.getId() != null && specItem.getId() > 0) {
                if (specItem.getDeleted() == 1) {
                    if (!removeById(specItem.getId())) {
                        return false;
                    }
                } else {
                    if (!updateById(specItem)) {
                        return false;
                    }
                }
            } else {
                specItem.setSpecCategoryId(goodsSpec.getSpecCategoryId());
                specItem.setSpecId(goodsSpec.getId());
                specItem.setId(null);
                if (!save(specItem)) {
                    return false;
                }
            }
        }
        return true;
    }

    @Transactional
    public boolean deleteBySpecIds(List<Long> specIds) {
        LambdaQueryWrapper<GoodsSpecItem> baseWrapper = new LambdaQueryWrapper<>();
        return remove(baseWrapper.in(GoodsSpecItem::getSpecId, specIds));
    }

}
